$(function () {
    layui.use(['table','layer'],function () {
        var table = layui.table;
        var layer = layui.layer;
        table.render({
            elem:'#LAY_table_account',
            url:'/pages/back/orderDe/accountList',
            toolbar: '#toolbarDemo',//开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports', 'print'],
            page:true,
            limit:10,
            cols:[[
                {field:'orid',title:'订单编号',sort:true},
                {field:'price',title:'订单价格'},
                {field:'atime',title:'对账时间',templet:function (d) {
                    if(!d.atime){
                        return "暂未对账"
                    }
                    return layui.util.toDateString(d.atime)
                    }},
                {field:'right',title:'操作',templet:"#barOption",sort:true},
            ]],
            id: 'testReload'
        })
        var $ = layui.$, active = {
            reload: function(){
                var orid = $('#demoReload').serialize();

                //执行重载
                table.reload('testReload', {
                    url:'/pages/back/orderde/findByOrid?'+orid,
                    where: {
                        key: {
                            name: orid
                        }
                    },
                });
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        table.on('tool(LAY_table_account)',function (obj) {
            var data = obj.data;
            if(obj.event == "edit"){
                var status = data.status;
                var orid = data.orid;
                if(status == 3){
                    layer.confirm("是否确定对账？",function () {
                        $.ajax({
                            url:'/pages/back/orderDe/aaccount?'+orid,
                            type:'post',
                            data:{"orid":orid},
                            success:function (data) {
                                if(data=="success"){
                                    layer.msg("对账成功",{time:1000,icon:1,end:function () {
                                            window.location.href="/pages/back/orderDe/aaccountListPre"
                                        }})
                                }else if (data=="isEmpty"){
                                    layer.msg("数据有误",{time:1000,icon:2})
                                }else{
                                    layer.msg("对账失败",{time:1000,icon:2})
                                }
                            }
                        })
                    })
                }else{
                    layer.msg("订单尚未完结")
                }
            }
        })
    })
})